#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QCloseEvent>
#include<QSettings>
#include<QLayout>
#include<QTimer>
#include<QTcpSocket>
#include<QInputDialog>
#include<QListWidgetItem>
#include<QMessageBox>
#include "messagepackage.h"
#include "audiohandler.h"



class cameraHandler;  // 前向声明
class videoSender;    // 前向声明
class videoRecver;    // 前向声明

namespace Ui {
class MainWindow;
}



class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QTcpSocket *socket,QWidget *parent = nullptr);
    ~MainWindow();
    QString client_user="";
    QString getClient_user() const;
    //void updateGroupList(const QJsonArray& groups);//更新组列表

    //文件  相关
    void refreshFileList();



public slots:
    void update_onlUser(QString name);
    void setClient_user(const QString &value);
    void on_recvSingleMessage(MessagePackage& pack);

    //讨论组
    void on_add_group_clicked();
    void on_groupListReceived(const QJsonArray &groups);
    void on_groupMessageReceived(MessagePackage &pack);
    void on_groupJoined(bool success, const QString &groupId, const QString &groupName);
    void on_groupMemberAdded(const QString &groupId, const QString &username);
    void on_groupListClicked(QListWidgetItem *item);
    void onGroupCreated(bool success, const QString &groupId, const QString &groupName);
    void onFileDataReceived(const QString &filename, const QByteArray &data);
    void onFileListReceived(const QJsonArray &files);

    //文件相关
    void onFileUploadSuccess();
protected:
    void closeEvent(QCloseEvent *close_ev) override;
private slots:
    void on_quit_pb_triggered();
    void updateNavButtons(int index);
    void on_updateList();
    void on_sendButton_clicked();
    void onCurrentChanged();

    //void on_add_group_clicked();




    void on_btnRefresh_clicked();

    void on_btnUpload_clicked();

    void on_bunDownload_clicked();
//发送
    void on_send_pb_clicked();

    void on_recv_pb_clicked();
    void on_videoUpdated(QImage image);
    void on_horizontalSlider_valueChanged(int value);
    //接收
    void videoSet(QImage image);

    //自己逻辑
    void createMeeting();           // 槽函数：创建会议
    void joinMeeting();             // 槽函数：加入会议
    void onMeetingCreated(MessagePackage ipport_pack);
    void onMeetingJoined(MessagePackage pack);

private:
    QTcpSocket *socket=nullptr;
    Ui::MainWindow *ui;
    QAction *actChat;
    QAction *actMeeting;
    QAction *actFile;
    void window_init();
    void readSettings();
    QTimer *updateList_timer=nullptr;
    //组相关
    void updateGroupList(const QJsonArray &groups);
    QString currentGroupId;
    bool isGroupChat = false;




    //视频 相关
    cameraHandler* chandler;

    videoSender * sender;
    //接收
    videoRecver* recver;
    QThread *thread;

    //自己的视频逻辑
    videoSender *my_videoSender=nullptr;

    //音频
    AudioSender *audioSender=nullptr;
    AudioRecver *audioRecver=nullptr;

signals:
    void mainQuit();
};

#endif // MAINWINDOW_H
